package cqrtplm.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import cqrtplm.dto.RtCfgBomDTO;
import cqrtplm.dto.query.BomQueryDTO;
import cqrtplm.entity.RtCfgBomDO;
import cqrtplm.vo.*;
import feign.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.math.BigInteger;
import java.util.List;
import java.util.Map;

/**
* @author 博
* @description 针对表【RT_CFG_BOM(配置管理-需求状态表-成品BOM表)】的数据库操作Mapper
* @createDate 2025-08-25 23:24:01
* @Entity entity.RtCfgBom
*/
public interface RtCfgBomMapper extends BaseMapper<RtCfgBomDO> {

    List<BuildEbomInfoVO> batchIbaByoids(@Param("oids") List<String> oids);

    int batchInsert(@Param("list") List<RtCfgBomDO> list);

//    List<LikeBomVO> getRtCfgBOMByPartNumber(@Param("partNumber")String partNumber);

    int updateEbomPartNumberByPartNumber(RtCfgBomDTO rtCfgBomDTO);

    List<RtCfgBomDO> getRtCfgBuildBOMList(@Param("statusid")String statusid,@Param("viewName") String viewName);

    @Update("update RT_CFG_STATUS_INFO set BOMOID=#{bomoid} where STATUSID=#{infoid}")
    void updateStatusInfoByBomOid(@Param("infoid") String infoid,@Param("bomoid") BigInteger bomoid);

    @Select("SELECT \n" +
            "\tt2.OID AS parentMasterId,\n" +
            "\tt2.PARTNUMBER AS parentNumber,\n" +
            "\tt1.NAME AS name,\n" +
            "\tt1.VIEWDEFOID AS viewID,\n" +
            "\tt1.OTYPE AS otype\n" +
            "FROM\n" +
            "\tTY_PART t1\n" +
            "\tINNER JOIN TY_PARTMASTER t2 ON t1.MASTEREDOID = t2.OID \n" +
            "\tinner join TY_VIEWDEF t3 on t1.VIEWDEFOID = t3.oid\n" +
            "WHERE\n" +
            "\tt2.PARTNUMBER =#{partnumber}\n" +
            "\tAND t1.ISLASTITERATED =1\n" +
            "\tand t3.name='Design' and t1.LOCKSTATEINFO='wrk'")
    Map<String, Object> selectParentByBomOidOut(@Param("partnumber")String partnumber);

    @Select("SELECT \n" +
            "\tt2.OID AS parentMasterId,\n" +
            "\tt2.PARTNUMBER AS parentNumber,\n" +
            "\tt1.NAME AS name,\n" +
            "\tt1.VIEWDEFOID AS viewID,\n" +
            "\tt1.OTYPE AS otype\n" +
            "FROM\n" +
            "\tTY_PART t1\n" +
            "\tINNER JOIN TY_PARTMASTER t2 ON t1.MASTEREDOID = t2.OID \n" +
            "\tinner join TY_VIEWDEF t3 on t1.VIEWDEFOID = t3.oid\n" +
            "WHERE\n" +
            "\tt2.PARTNUMBER =#{partnumber}\n" +
            "\tAND t1.ISLASTITERATED =1\n" +
            "\tand t3.name='Design' and t1.LOCKSTATEINFO='c/i'")
    Map<String, Object> selectParentByBomOidIn(@Param("partnumber")String partnumber);


    @Select("SELECT OID as masterId from TY_PARTMASTER where PARTNUMBER=#{partnumber}")
    String selectMasterIdByNumber(@Param("partnumber")String partnumber);

    @Select("SELECT t2.OID as oid from TY_PARTMASTER t1 inner join TY_PART t2 on t1.OID=t2.MASTEREDOID " +
            "where t2.PARTNUMBER=#{partnumber} and t2.ISLASTITERATED=1 and t2.LOCKSTATEINFO='wrk'")
    String selectOidByNumberOut(@Param("partnumber")String partnumber);

    @Select("SELECT t2.OID as oid from TY_PARTMASTER t1 inner join TY_PART t2 on t1.OID=t2.MASTEREDOID " +
            "where t2.PARTNUMBER=#{partnumber} and t2.ISLASTITERATED=1 and LOCKSTATEINFO='c/i'")
    String selectOidByNumberIn(@Param("partnumber")String partnumber);

    List<RtCfgBomDO> selectListByInfoId(String statusid);

    BomConfigurationVO selectProductTypeAndCustomerBystatusid(String statusid);

    BigInteger getLatestRtCfgBom(BomConfigurationVO bomConfigurationVO);

    int manualAdjustmentOfGroupPowerStructure(RtCfgBomDTO rtCfgBomDTO);

    @Select("select OTYPE as otype from RT_CFG_BOM where PARTNUMBER=#{partnumber} and IS_IMPORT_SAP=1")
    List<RtCfgBomDO> selectIsImportSapByNumber(@Param("partnumber") String partnumber);

    @Update("update RT_CFG_BOM set IS_IMPORT_SAP=1 where PARTNUMBER=#{partnumber}")
    void updateIsImportSapByNumber(@Param("matnr")String matnr);

    @Select("SELECT t2.OID as oid,t3.ATTRIBUTEVALUE as attributevalue from TY_PARTMASTER t1 " +
            "inner join TY_PART t2 on t1.OID=t2.MASTEREDOID " +
            "inner join VIEW_TY_IBA t3 on t2.OID=t3.OBJBASEOID " +
            "where t1.PARTNUMBER=#{drawingStr} and t2.ISLASTITERATED='1' and t2.LOCKSTATEINFO='wrk'")
    Map<String,String> selectAttributevalueByNumberOut(String drawingStr);

    @Select("SELECT t2.OID as oid,t3.ATTRIBUTEVALUE as attributevalue from TY_PARTMASTER t1 " +
            "inner join TY_PART t2 on t1.OID=t2.MASTEREDOID " +
            "inner join VIEW_TY_IBA t3 on t2.OID=t3.OBJBASEOID " +
            "where t2.OID=#{oid} and t3.ATTRIBUTENAME=#{name} and t2.ISLASTITERATED='1' and t2.LOCKSTATEINFO='c/i'")
    Map<String,String> selectAttributevalueByNumberIn(String oid,String name);

    RtCfgBomPartDetailVO getPartDetails(String nowOid);

    String replacePartNumber(@Param("partNumber") String partNumber,@Param("templateCode") String templateCode);

    String getTemplateCode(String oid);

    List<RtCfgMzVO> selectBytemplateCode(String templateCode);
}




